package com.java.business.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.java.business.dao.CheckinMapper;
import com.java.business.dao.MaintainDetailMapper;
import com.java.business.dao.MaintainRecordMapper;
import com.java.business.model.Checkin;
import com.java.business.model.MaintainDetail;
import com.java.business.model.MaintainRecord;
import com.java.business.service.MaintainDetailService;
import com.java.common.model.Result;
import com.java.manage.model.Accessories;
import com.java.manage.model.Company;
import com.java.manage.model.Maintain;
import com.java.manage.service.api.AccessoriesApiService;
import com.java.manage.service.api.CompanyApiService;
import com.java.manage.service.api.MaintainApiService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class MaintainDetailServiceImpl implements MaintainDetailService {

    @Autowired
    private MaintainDetailMapper maintainDetailMapper;

    @Autowired
    private MaintainRecordMapper maintainRecordMapper;

    @DubboReference
    private MaintainApiService maintainApiService;
    @DubboReference
    private AccessoriesApiService accessoriesApiService;


    @Override
    public Result list(MaintainDetail maintainDetail) {
        PageHelper.startPage(maintainDetail.getPageNum(), maintainDetail.getPageSize());
        List<MaintainDetail> list = maintainDetailMapper.list(maintainDetail);

        // 维修保养项目表
        List<Integer> maintainCollect = list.stream().map(MaintainDetail::getMaintainId).collect(Collectors.toList());
        List<Maintain> maintains = maintainApiService.listByIds(maintainCollect);
        Map<Integer, Maintain> maintainMap = maintains.stream().collect(Collectors.toMap(maintain -> maintain.getId(), maintain -> maintain));

        list.stream().forEach(app -> {
            app.setMaintain(maintainMap.get(app.getMaintainId()));
        });

        PageInfo<MaintainDetail> pageInfo = new PageInfo<>(list);

        Map<String, Object> map = new HashMap<>();
        map.put("list", pageInfo.getList());
        map.put("total", pageInfo.getTotal());
        return Result.success(map);
    }

    @Override
    public Result lists(Integer id) {
        List<MaintainDetail> list = maintainDetailMapper.lists(id);

        // 维修保养项目表
        List<Integer> maintainCollect = list.stream().map(MaintainDetail::getMaintainId).collect(Collectors.toList());
        List<Maintain> maintains = maintainApiService.listByIds(maintainCollect);
        Map<Integer, Maintain> maintainMap = maintains.stream().collect(Collectors.toMap(maintain -> maintain.getId(), maintain -> maintain));

        list.stream().forEach(app -> {
            app.setMaintain(maintainMap.get(app.getMaintainId()));
        });

        PageInfo<MaintainDetail> pageInfo = new PageInfo<>(list);

        Map<String, Object> map = new HashMap<>();
        map.put("list", pageInfo.getList());
        map.put("total", pageInfo.getTotal());
        return Result.success(map);
    }

    @Override
    public Result save(MaintainDetail maintainDetail) {
        maintainDetailMapper.save(maintainDetail);
        return Result.success();
    }

    @Override
    public Result modify(MaintainDetail maintainDetail) {
        maintainDetailMapper.modify(maintainDetail);
        return Result.success();
    }

    @Override
    public Result remove(int[] ids) {
        for (int id : ids) {
            maintainDetailMapper.remove(id);
        }
        return Result.success();
    }

    @Override
    public Result get(int id) {
        maintainDetailMapper.get(id);
        return Result.success();
    }
}
